
package com.hidt.shop.controller;

import com.hidt.shop.domain.User_table;
import com.hidt.shop.service.UserService;
import com.hidt.shop.util.SafeUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.Console;
import java.io.File;
import java.io.IOException;
import java.util.*;

@Controller
public class UserController {
    @Autowired
    private UserService userService;
/***
     * 增加用户
     * @param user_table
     * @return
     */
    @RequestMapping("/adduser")
    @ResponseBody
    public Map AddUser(@RequestBody User_table user_table) {
        System.out.println(user_table);
        int i = userService.AddUser(user_table);
        HashMap map=new HashMap();
        if(i>0){
            map.put("mes","注册成功");
        }else{
            map.put("mes","注册失败");
        }
        return map;
    }
//    /***
//     * 根据用户名查找用户
//     * @param user_name
//     * @return
//     */
//    @RequestMapping("/finduserbyuser")
//    public String FinnUserByUser(String user_name ){
//        if(userService.isUserExist(user_name)){
//            System.out.println("用户名存在");
//        }
//        return "aaa";
//    }

/***
     * 会员登录
     * @param map
     * @return
     */
    @RequestMapping(value = "/userlogin")
    @ResponseBody
//    public String UserLogin(HttpServletRequest request) throws JsonProcessingException {
    public Map UserLogin(@RequestBody Map map){
        System.out.println(map.get("user_name"));
        int i = userService.UserLogin((String) map.get("user_name"),(String) map.get("user_pwd"));
        HashMap map1 = new HashMap();
        if(i==2) {
            System.out.println("登录成功");
            System.out.println("111");
            int id = userService.findUserByUsername((String) map.get("user_name")).getId();
            System.out.println(id);
            map1.put("id",id);
            map1.put("mes","登陆成功");
            return map1;
//            return new ResultInfo(2,"登录成功");
        }
        else if(i==0) {
            System.out.println("用户名不存在");
            map1.put("mes","用户名不存在");
            return map1;
//            return new ResultInfo(0,"用户名不存在");
        }else {
                System.out.println("密码错误");
            map1.put("mes","密码错误");
            return map1;
//            return new ResultInfo(1,"密码错误");
        }
    }
    /***
     * 会员修改自己基础信息
     * @param user_table,user_image
     *
     * @return String
     *@创建人  lmf
     *@创建时间  2021/1/19
     */
    @RequestMapping("/updateuserbyid")
    @ResponseBody
    public String UpdateUserById(User_table user_table,@RequestParam(value = "filepath" ,required =false)MultipartFile user_image,HttpServletRequest req){
        if (user_image != null) {
            // 指定文件上传后的物理位置
            String path = "D:\\img";
            // 获取文件名
            String filename = user_image.getOriginalFilename();
            // 改名字
            String newfilename = UUID.randomUUID() + "-"+filename;
            File f = new File(path, newfilename);
            try {
                if (!f.exists()) {
                    System.out.println(req.getServletContext().getRealPath("/image/")+newfilename);
                    FileUtils.copyInputStreamToFile(user_image.getInputStream(),new File(req.getServletContext().getRealPath("/image/")+newfilename));
                    // 上传文件
                    user_image.transferTo(f);
                    user_table.setUser_image(newfilename);
                }
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        int i = userService.UpdateUserByIds(user_table);
        System.out.println("11111");
        if(i>0){
            System.out.println("修改成功");
            return "/login.jsp";
        }else {
            return "/adduser.jsp";
        }
    }
    /***
     * 会员修改密码
     * @param user_name,user_pwd，newuser_pwd,model
     * @return String
     *@创建人  lmf
     *@创建时间  2021/1/19
     */
    @RequestMapping("/updatepwd")
    public String UpdatePwdByUsername(@RequestParam("user_name") String user_name,
                                      @RequestParam("user_pwd") String user_pwd,
                                      @RequestParam("newuser_pwd") String newuser_pwd,
                                      ModelMap model){
            if(!userService.isUserExist(user_name)){
                model.addAttribute("msg","用户名不存在");
            }else {
                if (SafeUtil.encode(user_pwd).equals(userService.getPasswordByUsername(user_name))) {

                    userService.UpdatePwdByUsername(user_name,SafeUtil.encode(newuser_pwd));
                    model.addAttribute("msg", "修改密码成功！");
                    System.out.println("修改密码成功");
                } else {
                    model.addAttribute("msg", "密码错误！");
                }
            }
        return "/updatepwd.jsp";
    }

    /**
     * 管理员全查会员信息
     *@参数
     *@返回值  List<User_table>
     *@创建人  lmf
     *@创建时间  2021/1/20
     */
    @RequestMapping("/findalluser")
    @ResponseBody
    public List<User_table> FindAllUser(){
        List<User_table> users = userService.FindAllUser();
//        m.addAttribute("users",users);
        return users;
    }

    /**
     * 管理员逻辑删除用户
     *@参数   int
     *@返回值  String
     *@创建人  lmf
     *@创建时间  2021/1/20
     */
    @RequestMapping("/deleteuser")
    @ResponseBody
    public String DeleteUser(@RequestBody Map map){
        int id = (Integer) map.get("id");
        int i = userService.DeleteUser(id);
        if(i>0){
            return "删除成功";
        }else {
            return "删除失败";
        }
    }

    /**
     * 通过id得到用户
     *@参数   int
     *@返回值  String
     *@创建人  lmf
     *@创建时间  2021/1/20
     */
    @RequestMapping("/selectbyid")
    @ResponseBody
    public User_table findUserById(@RequestBody Map map){
        int id=(Integer) map.get("id");
        User_table user = userService.findUserById(id);
        return user;
    }

    /**
     * 通过用户名得到用户
     *@参数   int
     *@返回值  String
     *@创建人  lmf
     *@创建时间  2021/1/20
     */
    @RequestMapping("/finduserbyname")
    @ResponseBody
    public Map findUserByUsername(@RequestBody Map map){
        String user_name=(String) map.get("user_name");
        User_table user = userService.findUserByUsername(user_name);
        HashMap map1=new HashMap();
        if(user==null){
            map1.put("mes","用户名不存在");
        }else {
            map1.put("mes","用户名已存在");
        }
        return map1;
    }
}

